package day11;

import java.util.concurrent.locks.ReentrantLock;

//b.通过Lock锁编写三个线程 让其出现死锁的案例,
// 提示: 添加3个对象  每个线程 分别锁两个对象 让其构成一个循环链
// 相互等待  可以参考 课上的案例
public class Home4 {
    static ReentrantLock lock1=new ReentrantLock();
    static ReentrantLock lock2=new ReentrantLock();
    static ReentrantLock lock3=new ReentrantLock();
    public static void main(String[] args) {
        Home4 h4=new Home4();
        String name=Thread.currentThread().getName();
            new Thread(()->{
                lock1.lock();
                System.out.println("线程1获得lock1的锁");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock2.lock();
                System.out.println("线程1获得lock1,lock2的锁");
                lock2.unlock();
                System.out.println("线程1释放lock2的锁");
                lock1.unlock();
                System.out.println("线程1释放lock1的锁");
            }).start();
            new Thread(()->{
            lock2.lock();
            System.out.println("线程2获得lock2的锁");
            lock1.lock();
            System.out.println("线程2获得lock2,lock1的锁");
            lock1.unlock();
            System.out.println("线程2释放lock1的锁");
            lock2.unlock();
            System.out.println("线程2释放lock2的锁");
        }).start();
        new Thread(()->{
            lock3.lock();
            System.out.println("线程3获得lock3的锁");
            lock1.lock();
            System.out.println("线程3获得lock3,lock1的锁");
            lock1.unlock();
            System.out.println("线程3释放lock1的锁");
            lock3.unlock();
            System.out.println("线程3释放lock3的锁");
        }).start();

    }
}
